Skip to content

Speed up counting in statistics.fmean()#148875

Merged
rhettinger merged 2 commits intopython:mainfrom
rhettinger:fmean_optimization
Apr 23, 2026
Merged

Speed up counting in statistics.fmean()#148875
rhettinger merged 2 commits intopython:mainfrom
rhettinger:fmean_optimization

Conversation

@rhettinger
Copy link
Copy Markdown
Contributor

Implement a faster counting strategy that avoids tuple creation, indexing, and destruction.

Baseline timing:

% ./python.exe -m timeit -s 'from statistics import fmean' 'fmean(iter(range(10**3)))'
20000 loops, best of 5: 18.7 usec per loop

After the PR:

% ./python.exe -m timeit -s 'from statistics import fmean' 'fmean(iter(range(10**3)))'
20000 loops, best of 5: 11.4 usec per loop

Comment thread Lib/statistics.py Outdated
Co-authored-by: Shrey Naithani <shrey.naithani@shelllite.tech>
@rhettinger rhettinger merged commit fbc7676 into python:main Apr 23, 2026
48 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Performance or resource usage skip issue skip news

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants